<template>
  <el-dialog :title="'收款单明细'" :model-value="dialog" @close="close" width="80%">
    <div class="detail-container">
      <el-table :data="orderList" style="width: 100%" size="small" v-loading="loading">
        <el-table-column prop="orderNumber" label="订单号" width="180" />
        <el-table-column prop="orderType" label="订单类型">
          <template #default="scope">{{ orderTypeMap.get(scope.row.orderType) || '-' }}</template>
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态">
          <template #default="scope">{{ orderStatusMap.get(scope.row.orderState) || '-' }}</template>
        </el-table-column>
        <el-table-column prop="modelType" label="车型类型">
          <template #default="scope">{{ priceModelTypeMap.get(scope.row.modelType) || '-' }}</template>
        </el-table-column>
        <el-table-column prop="passengerName" label="乘客姓名" />
        <el-table-column prop="departureAddress" label="出发地" min-width="150" />
        <el-table-column prop="destinationAddress" label="目的地" min-width="150" />
        <el-table-column prop="serviceStartTime" label="服务开始时间" width="150">
          <template #default="scope">{{ formatTime(scope.row.serviceStartTime) }}</template>
        </el-table-column>
        <el-table-column prop="serviceEndTime" label="服务结束时间" width="150">
          <template #default="scope">{{ formatTime(scope.row.serviceEndTime) }}</template>
        </el-table-column>
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createTime" label="创建时间" width="150">
          <template #default="scope">{{ formatTime(scope.row.createTime) }}</template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination-container" v-if="total > 0">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="query.pageNum" :page-sizes="[10, 20, 50, 100]" :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="total" />
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { formatTime } from '@/utils'
import { querySettlementDetailPage } from '@/api/settlement'
import { orderTypeMap, orderStatusMap, priceModelTypeMap } from '@/config/constants'

const props = defineProps<{
  dialog: boolean
  Id: string
}>()

const emit = defineEmits<{
  'update:dialog': [value: boolean]
  'update:currentForm': [value: object]
}>()

const loading = ref(false)
const settlementInfo = ref<any>(null)
const orderList = ref<any[]>([])
const total = ref(0)
const query = reactive({
  pageNum: 1,
  pageSize: 10,
})

const close = () => {
  emit('update:dialog', false)
  // 只关闭弹窗并清理内部数据，不再清空父组件的currentForm
  settlementInfo.value = null
  orderList.value = []
  total.value = 0
}

const loadDetail = () => {
  if (!props.Id) {
    return
  }

  loading.value = true
  querySettlementDetailPage({ settlementUuid: props.Id, ...query }).then((res: any) => {
    if (res.code == 200) {
      // 假设API返回的数据结构包含settlementInfo和orderList
      if (res.data) {
        // 如果API返回的是包含list和total的数据结构
        if (res.data.list) {
          orderList.value = res.data.list
          total.value = res.data.total || 0
          // 如果结算信息在其他字段中，需要根据实际API返回结构调整
          settlementInfo.value = res.data.settlementInfo || {}
        } else {
          // 兼容旧的数据结构
          orderList.value = [res.data]
          total.value = 1
        }
      }
    }
  }).catch(() => {
    ElMessage.error('获取订单详情失败')
  }).finally(() => {
    loading.value = false
  })
}

// 分页处理函数
const handleSizeChange = (newSize: number) => {
  query.pageSize = newSize
  query.pageNum = 1
  loadDetail()
}

const handleCurrentChange = (newPage: number) => {
  query.pageNum = newPage
  loadDetail()
}

onMounted(() => {
  if (props.dialog && props.Id) {
    loadDetail()
  }
})
</script>

<style scoped>
.detail-container {
  max-height: 600px;
  overflow-y: auto;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mb-4 {
  margin-bottom: 16px;
}

.card-header {
  font-weight: bold;
}

.loading-container,
.empty-container {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 让el-descriptions组件的标签列和内容列等宽 */
/* :deep(.el-descriptions__label) {
  width: 50% !important;
  flex: 0 0 50% !important;
} */

/* 让el-descriptions组件的标签列和内容列等宽 */
:deep(.el-descriptions__content) {
  width: 30% !important;
  flex: 0 0 50% !important;
}

/* 表格样式优化 */
:deep(.el-table) {
  margin-bottom: 20px;
}
</style>
